<?php


namespace app\site\controller\auth;


use app\common\exception\MiException;
use app\model\site\SiteUser;
use app\model\site\SiteUserGroupAccess;
use app\site\controller\AController;
use app\site\service\auth\AdminService;
use app\site\service\auth\SiteUserService;
use app\site\service\RequestService;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Request;

class SiteUserController extends AController {


    /**
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws DataNotFoundException
     */
    public function index()
    {
        return $this->reqSer->setService(SiteUserService::instance())->setPaginate()->getResponse();
    }

    /**
     * @throws MiException
     */
    public function add(Request $request){
        if ($request->isPost()){
            $post = $request->post();
            SiteUserService::instance()->saveInfo($post,'add');
            return jsonSuccess();
        }
    }

    /**
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws DataNotFoundException
     * @throws MiException
     */
    public function edit(Request $request): \think\response\Json
    {
        if($request->isPost()){
            $post = $this->post();
            SiteUserService::instance()->saveInfo($post, 'edit');
            return jsonSuccess();
        }else{
            return $this->reqSer->setService(SiteUserService::instance())->setFind()->getResponse();
        }
    }


    public function del(): \think\response\Json
    {
        $ids = request()->param('ids');
        if (!$ids || count($ids)==0){
            return jsonError('请选择要删除的记录');
        }
        if (in_array('1', $ids)){
            return jsonError("不能删除【admin】");
        }
        SiteUser::where('id', 'in', $ids)->delete();
        SiteUserGroupAccess::where('uid', 'in', $ids)->delete();

        return jsonSuccess();
    }


}
